package com.horse.cloud.member.user.service;

import com.horse.cloud.data.access.memberuser.entity.ClMemberUser;
import com.horse.cloud.data.access.memberuser.mapperInterface.ClMemberUserMapper;
import com.horse.cloud.framework.base.BaseService;
import com.horse.cloud.framework.base.callback.DoTransactionContent;
import com.horse.cloud.framework.general.response.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @create : Created by intelliJ IDEA 17.2
 * @author : bing.Pan
 * @e-mail : 15923508369@163.com
 * @gmdate : 09 28 2017 15:51
 * @sidesc :
 */
@Service
public class TestService extends BaseService {

    @Autowired private ClMemberUserMapper memberUserMapper;


    public Object testTransaction() {
        Response<Object> objectResponse = new Response<>();
        return withTransaction(new DoTransactionContent() {
            @Override
            public Response execute() throws Exception {

                Object testTransaction = redisClient.get("testTransaction");
                LOGGER.info("【testTransaction】【redis中 计数值为】:{}",testTransaction);
                int transaction = testTransaction ==null? 1:(int)testTransaction;

                ++transaction;
                System.out.println("transaction:"+ transaction);
                ClMemberUser memberUser = new ClMemberUser();
                memberUser.setId(transaction);
                memberUser.setRealName("realName" + transaction);
                memberUser.setLoginName("loginName" + transaction);

                memberUserMapper.insert(memberUser);

                redisClient.set("testTransaction", transaction);

                if (transaction % 2 == 0) {
                    LOGGER.info("【TestService】【testTransaction】 测试事物回滚");
                } else {
                    LOGGER.info("【TestService】【testTransaction】 测试事物提交");
                    return new Response<>("测试事物提交");
                }
                return null;
            }
        }.logTitle("测试事物").logMethod("testTransaction").requestArgs(objectResponse).recordTransactionContentLog());
    }
}
